package com.douqu.game.core.entity.world;

import com.bean.core.buffer.ByteBuffer;
import com.bean.core.util.Utils;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.ext.data.BaseData;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.FunctionFactory;
import com.douqu.game.core.util.CoreUtils;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.util.StringUtils;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2017-10-24 17:02
 */
public class WorldArenaData extends BaseData {

    /**
     * 竞技场排行榜对象
     */
    private List<WorldArenaBean> arenaRankList = new CopyOnWriteArrayList<>();

    /**
     * 昨天的排行榜
     */
    private List<WorldArenaBean> yesterdayArenaRankList = new CopyOnWriteArrayList<>();

    private long lastCopyTime;


    @Override
    public void init() {

    }

    @Override
    public void checkInit() {

    }



    @Override
    public void reset()
    {
        copyRankToYesterdayRank();
    }

    @Override
    public void checkReset() {
        //第一次启动服务器的时候不执行任何操作
        if (lastCopyTime == 0)
            return;
        if(!CoreUtils.isTodayByOffset(lastCopyTime, DataFactory.currentTime, ConstantFactory.DEFAULT_RESET_RECEIVE_REWARD_HOUR)){
            reset();
        }
    }

    @Override
    public void writeTo(ByteBuffer buffer)
    {
        int size = arenaRankList.size();
        buffer.writeInt(size);
        for(int i = 0; i < size; i++)
        {
            arenaRankList.get(i).writeTo(buffer);
        }
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        size = yesterdayArenaRankList.size();
        buffer.writeInt(size);
        for(int i = 0; i < size; i++)
        {
            yesterdayArenaRankList.get(i).writeTo(buffer);
        }
        buffer.writeLong(lastCopyTime);
    }

    @Override
    public void loadFrom(ByteBuffer buffer)
    {
        arenaRankList.clear();

        int size = buffer.readInt();
        WorldArenaBean arenaRank = null;
        for(int i = 0; i < size; i++)
        {
            arenaRank = new WorldArenaBean();
            arenaRank.loadFrom(buffer);
            if(Utils.isNullOrEmpty(arenaRank.getObjectIndex())){
                continue;
            }
            arenaRankList.add(arenaRank);
        }

        Set<WorldArenaBean> arenaRankSet = new LinkedHashSet<>(arenaRankList);
        arenaRankList.clear();
        arenaRankList.addAll(arenaRankSet);
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        yesterdayArenaRankList.clear();
        size = buffer.readInt();
        for(int i = 0; i < size; i++)
        {
            arenaRank = new WorldArenaBean();
            arenaRank.loadFrom(buffer);
            yesterdayArenaRankList.add(arenaRank);
        }
        Set<WorldArenaBean> yesArenaRankSet = new LinkedHashSet<>(yesterdayArenaRankList);
        yesterdayArenaRankList.clear();
        yesterdayArenaRankList.addAll(yesArenaRankSet);
        lastCopyTime = buffer.readLong();
    }


    /**
     * 保存前一天的排行记录
     */
    private void copyRankToYesterdayRank(){
        LogUtils.warn("arenaRank reset start .... ");
        yesterdayArenaRankList.clear();
        for(WorldArenaBean bean : arenaRankList){
            yesterdayArenaRankList.add(new WorldArenaBean(bean));
        }
        lastCopyTime = DataFactory.currentTime;
    }

    public int getArenaRankByObjectIndex(String objectIndex){
        int rank = arenaRankList.indexOf(new WorldArenaBean(objectIndex)) + 1;
        if(rank == 0){ // 排行榜还没有自己
            addArenaRankInfo(objectIndex);
            return arenaRankList.size();
        }

        return rank;
    }

    /**
     * 重载获取竞技场排行的方法,如果等级不够 return0
     * @param player
     * @return
     */
    public int getArenaRankByObjectIndex(Player player){
        if(!CoreUtils.isFunctionUnLock(FunctionFactory.UNLOCK_COMPETITIVE_SYSTEM, player)){
            return 0;
        }
        int rank = arenaRankList.indexOf(new WorldArenaBean(player.getObjectIndex())) + 1;
        if(rank == 0){ // 排行榜还没有自己
            addArenaRankInfo(player.getObjectIndex());
            return arenaRankList.size();
        }

        return rank;
    }

    public int getYesterdayArenaRankByObjectIndex(String objectIndex){
        int rank = yesterdayArenaRankList.indexOf(new WorldArenaBean(objectIndex)) + 1;
        return rank;
    }

    public void deleteArenaRankByObjectIndex(String objectIndex){
        if(StringUtils.isNullOrEmpty(objectIndex)){
            return;
        }
        arenaRankList.remove(new WorldArenaBean(objectIndex));
    }

    /**
     * 添加一个排名
     * @param objectIndex
     * @return
     */
    public WorldArenaBean addArenaRankInfo(String objectIndex)
    {
        WorldArenaBean rankInfo= new WorldArenaBean();
        rankInfo.setRank(arenaRankList.size() + 1);
        rankInfo.setObjectIndex(objectIndex);



        arenaRankList.add(rankInfo);

        return rankInfo;
    }


    /**
     * 根据排名获取排行榜信息
     * @param rank
     * @return
     */
    public WorldArenaBean getArenaRankInfo(int rank){
        int index = rank - 1;
        WorldArenaBean rankInfo = index < arenaRankList.size()? arenaRankList.get(index) : null ;
        return rankInfo;
    }

    /**
     * 根据指定排名下的所有玩家
     * @return
     */
    public List<WorldArenaBean> getUnderTargetRankList(int maxRank){
        maxRank = Math.min(maxRank, arenaRankList.size());
        return arenaRankList.subList(0, maxRank);
    }

    /**
     * 根据指定排名区间的玩家
     * @return
     */
    public List<WorldArenaBean> getUnderTargetRankList(int value1,int value2){
        int maxValue = value1;
        int minValue = value2;
        if (value1 < value2){
            maxValue = value2;
            minValue = value1;
        }
        maxValue = maxValue > arenaRankList.size() ? arenaRankList.size() : maxValue;
        if (maxValue< minValue){
            return  new ArrayList<>();
        }
        List<WorldArenaBean> list = arenaRankList.subList(minValue, maxValue);
        if (list == null) {
            list =  new ArrayList<>();
        }
        return list;
    }

    /**
     * 根据两个排行榜名次，交换排行榜信息
     * @param rank1
     * @param rank2
     * @return
     */
    public synchronized boolean swapArenaRank(int rank1, int rank2){
        WorldArenaBean rankInfo1 = rank1 - 1 < arenaRankList.size()? arenaRankList.get(rank1 - 1) : null ;
        WorldArenaBean rankInfo2 = rank2 - 1 < arenaRankList.size()? arenaRankList.get(rank2 - 1) : null ;
        LogUtils.info("竞技场交换排名->rank1:", rank1, ">rankInfo1:", rankInfo1, ", rank2:", rank2, ", rankInfo2:", rankInfo2);
        if(rankInfo1 != null && rankInfo2 != null){
            rankInfo1.setRank(rank2);
            rankInfo2.setRank(rank1);
            Collections.swap(arenaRankList, rank1 - 1, rank2 - 1);
        }else {
            return false;
        }
        return true;
    }

    public List<WorldArenaBean> getArenaRankList() {
        return arenaRankList;
    }

    public void removePlayer(Player player)
    {
        int position = arenaRankList.indexOf(new WorldArenaBean(player.getObjectIndex()));
        if(position >= 0)
            arenaRankList.remove(position);

    }

    public void clear()
    {
        arenaRankList.clear();
    }


    public static void main(String[] args)
    {
        List<WorldArenaBean> list = new ArrayList<>();

        WorldArenaBean w1 = new WorldArenaBean();
        w1.setObjectIndex("123");
        w1.setRank(1);

        WorldArenaBean w2 = new WorldArenaBean();
        w2.setObjectIndex("234");
        w2.setRank(2);

        WorldArenaBean w3 = new WorldArenaBean();
        w3.setObjectIndex("234");
        w3.setRank(3);

        list.add(w1);
        list.add(w2);
        list.add(w3);

        Set set = new HashSet<>();
        set.addAll(list);

        System.out.println(list);


        System.out.println(set);


        list.clear();

        list.addAll(set);

        System.out.println(list);

    }
}
